package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.*;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.entity.vo.RecordVo;
import com.trace.app.mapper.RecordMapper;
import com.trace.app.service.*;
import com.trace.app.util.BeanCopyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Record)表服务实现类
 *
 * @author lionkliu
 * @since 2024-03-17 15:55:23
 */
@Service("recordService")
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {

    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;

    @Resource
    private RawService rawService;

    @Resource
    private SupplierService supplierService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();
        Page<Record> page = new Page<>(pageNum, pageSize);
        Page<Record> recordPage = page(page, queryWrapper);
        PageVo<Record> pageVo = new PageVo<>();
        pageVo.setResult(recordPage.getRecords());
        pageVo.setTotal(recordPage.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Record record) {
        return save(record) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Record record) {
        Record newRecord = new Record();
        newRecord.setId(record.getId())
                .setRawId(record.getRawId())
                .setUserId(record.getUserId());
        if (record.getRemarks() != null) {
            newRecord.setRemarks(record.getRemarks());
        }
        if (record.getImg() != null) {
            newRecord.setImg(record.getImg());
        }
        if (record.getLocation() != null) {
            newRecord.setLocation(record.getLocation());
        }
        updateById(newRecord);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getRecordsByProId(Integer rawId) {
        LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Record::getRawId, rawId);
        List<Record> records = list(queryWrapper);
        List<RecordVo> recordVoList = records.stream().map(record -> {
            RecordVo recordVo = BeanCopyUtils.copyBean(record, RecordVo.class);
            User user = userService.getById(record.getUserId());
            if (!Objects.isNull(user)) {
                Role role = roleService.getOne(new LambdaQueryWrapper<Role>().eq(Role::getId, user.getUserType()));
                if (!Objects.isNull(role)) {
                    recordVo.setUserType(role.getName());
                }
                recordVo.setUser(user);
            }

            return recordVo;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(recordVoList);
    }

    @Override
    public ResponseResult getRecordsByUserId(Integer userId) {
        LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Record::getUserId, userId);
        List<Record> records = list(queryWrapper);

        List<RecordVo> collect = records.stream().map(record -> {
            RecordVo recordVo = BeanCopyUtils.copyBean(record, RecordVo.class);

            LambdaQueryWrapper<Raw> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Raw::getId, record.getRawId());
            Raw raw = rawService.getOne(wrapper);
            recordVo.setRawName(raw.getName());

            return recordVo;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult search(Integer pageNum, Integer pageSize, Integer userId, Integer rawId) {
        LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(userId)) {
            queryWrapper.eq(Record::getUserId, userId);
        }
        if (!Objects.isNull(rawId)) {
            queryWrapper.eq(Record::getRawId, rawId);
        }
        Page<Record> userPage = new Page<>(pageNum, pageSize);
        page(userPage, queryWrapper);
        PageVo<Record> userPageVo = new PageVo<>();
        userPageVo.setTotal(userPage.getTotal());
        userPageVo.setPageTotal(userPage.getPages());
        userPageVo.setResult(userPage.getRecords());

        return ResponseResult.okResult(userPageVo);
    }

}

